package com.imooc.security.demo.config;

import com.imooc.security.demo.Filter.TimeFilter;
import com.imooc.security.demo.intercept.TimeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.Filter;
import java.util.ArrayList;

/**
 * @author JackYang
 * @description:
 * @create: 2020/6/13 23:49
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Autowired
    private TimeInterceptor timeInterceptor;
    
    
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(timeInterceptor);
    }
    
    /**
     * 添加针对Callable异步线程的拦截器。registry.addInterceptor这种方式对异步线程不起作用
     */
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        //configurer.registerCallableInterceptors() 添加针对Callable异步线程的拦截器
        //configurer.registerDeferredResultInterceptors() 添加针对DeferredResult异步线程的拦截器
    }
    
    //@Bean
    public FilterRegistrationBean createFilterRegistrationBe(){
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        TimeFilter timeFilter = new TimeFilter();
        filterFilterRegistrationBean.setFilter(timeFilter);
        
        //要映射的路径,可以根据需要个性化设置要映射的路径
        ArrayList<String> urlsList = new ArrayList<>();
        urlsList.add("/*");
        filterFilterRegistrationBean.setUrlPatterns(urlsList);
        return filterFilterRegistrationBean;
    }
}
